package org.dualr.litelog.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.dualr.litelog.dao.CategoryDao;
import org.dualr.litelog.dao.CommentDao;
import org.dualr.litelog.dao.LinkDao;
import org.dualr.litelog.dao.PostDao;
import org.dualr.litelog.dao.TagDao;
import org.dualr.litelog.entity.Category;
import org.dualr.litelog.entity.Comment;
import org.dualr.litelog.entity.Link;
import org.dualr.litelog.entity.Page;
import org.dualr.litelog.entity.Post;
import org.dualr.litelog.entity.Tag;
import org.dualr.litelog.utils.LiteLogUtil;

@SuppressWarnings("serial")
public class PostServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = req.getParameter("method");
		// category
		List<Category> categoryList = CategoryDao.getInstance().getListByProperty("type", "post", "string");
		req.setAttribute("categoryList", categoryList);
		// tag
		List<Tag> tagList = TagDao.getInstance().getAll();
		req.setAttribute("tagList", tagList);
		// pop post
		List<Post> popPostList = PostDao.getInstance().getPopPost(
				0, 6);
		req.setAttribute("PopPostList", popPostList);
		
		//view
		if (method.equals("view")) {
			String ids = req.getParameter("id");
			if(ids == null || "".equals(ids)){
				resp.sendRedirect("index");
				return;
			}
			Long id = Long.parseLong(ids);
			String path = "post.jsp";
			Post post = PostDao.getInstance().getByPostId(id);
			if(post != null){
				PostDao.getInstance().updateHitsorCommentCount(id, "hits", "increment");
			}
			int num1 = (int) (Math.random() * 10);
			int num2 = (int) (Math.random() * 10);
			req.setAttribute("num1", num1);
			req.setAttribute("num2", num2);
			HttpSession session = req.getSession();
			session.setAttribute("num1", num1);
			session.setAttribute("num2", num2);
			req.setAttribute("Post", post);
			// comments
			List<Comment> commentList = CommentDao.getInstance().getCommentByPostId(id);
			req.setAttribute("CommentList", commentList);
			//last post
			List<Post> lastPostList = PostDao.getInstance().getPostByRange(0, 6);
			req.setAttribute("LastPostList", lastPostList);
			try {
				req.getRequestDispatcher(path).forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
		}
		// list by page
		if (method.equals("list")) {
			// list
			int hits;
			hits = PostDao.getInstance().getCountByPropertyStatus("all", "","publish");
			int curPage = 1;
			if (req.getParameter("page") != null
					&& !"".equals(req.getParameter("page"))) {
				curPage = Integer.parseInt(req.getParameter("page"));
			}
			int pageSize = Integer.parseInt(LiteLogUtil.getInstance().getPropsValue("/litelog.properties", "litelog.pageSize"));
			Page page = LiteLogUtil.getInstance().pager(hits, pageSize, curPage);
			int indexStart = (curPage - 1) * pageSize;
			int indexEnd = curPage * pageSize;
			List<Post> postList = PostDao.getInstance().getPostByStatusRange(indexStart, indexEnd, "publish");
			req.setAttribute("PostList", postList);
			req.setAttribute("Page", page);
			req.setAttribute("pageMethod", "list");
			req.setAttribute("param", "list");
			// last comments
			List<Comment> recentCommentList = CommentDao.getInstance().getCommentByRange(0, 6);
			req.setAttribute("RecentCommentList", recentCommentList);
			// links
			List<Link> linkList = LinkDao.getInstance().getAll();
			req.setAttribute("linkList", linkList);
			try {
				req.getRequestDispatcher("index.jsp").forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
		}
		// category
		if (method.equals("category")) {
			String param = req.getParameter("param");
			if (param == null) {
				param = req.getParameter("value");
			}
			int hits;
			hits = PostDao.getInstance().getCountByPropertyStatus("categoryId",
					param,"publish");
			int curPage = 1;
			if (req.getParameter("page") != null
					&& !"".equals(req.getParameter("page"))) {
				curPage = Integer.parseInt(req.getParameter("page"));
			}
			int pageSize = Integer.parseInt(LiteLogUtil.getInstance().getPropsValue("/litelog.properties", "litelog.pageSize"));
			Page page = LiteLogUtil.getInstance().pager(hits, pageSize, curPage);
			int indexStart = (curPage - 1) * pageSize;
			int indexEnd = curPage * pageSize;
			List<Post> postList = PostDao.getInstance().getPostByPropertyStatus("categoryId", param, "publish", indexStart, indexEnd);
			//pop posts
			req.setAttribute("PostList", postList);
			req.setAttribute("Page", page);
			req.setAttribute("pageMethod", "category");
			req.setAttribute("param", param);
			// last comments
			List<Comment> recentCommentList = CommentDao.getInstance().getCommentByRange(0, 6);
			req.setAttribute("RecentCommentList", recentCommentList);
			// links
			List<Link> linkList = LinkDao.getInstance().getAll();
			req.setAttribute("linkList", linkList);
			try {
				req.getRequestDispatcher("index.jsp").forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
		}
		// tag
		if (method.equals("tag")) {
			String param = req.getParameter("param");
			if (param == null) {
				param = req.getParameter("value");
			}
			int hits;
			hits = PostDao.getInstance().getCountByPropertyStatus("tag", param, "publish");
			int curPage = 1;
			if (req.getParameter("page") != null
					&& !"".equals(req.getParameter("page"))) {
				curPage = Integer.parseInt(req.getParameter("page"));
			}
			int pageSize = Integer.parseInt(LiteLogUtil.getInstance().getPropsValue("/litelog.properties", "litelog.pageSize"));
			Page page = LiteLogUtil.getInstance().pager(hits, pageSize, curPage);
			int indexStart = (curPage - 1) * pageSize;
			int indexEnd = curPage * pageSize;
			List<Post> postList = PostDao.getInstance().getPostByPropertyStatus("tag", param, "publish", indexStart, indexEnd);
			req.setAttribute("PostList", postList);
			req.setAttribute("Page", page);
			req.setAttribute("pageMethod", "tag");
			req.setAttribute("param", param);
			// last comments
			List<Comment> recentCommentList = CommentDao.getInstance().getCommentByRange(0, 6);
			req.setAttribute("RecentCommentList", recentCommentList);
			// link
			List<Link> linkList = LinkDao.getInstance().getAll();
			req.setAttribute("linkList", linkList);
			try {
				req.getRequestDispatcher("index.jsp").forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			doGet(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
